import argparse
import numpy as np
import os
from scipy.misc import imread, imresize
#from imageio import imread
from keras import backend as K
import csv
import cv2
def read_transformed_image(image, image_size):

    img = image
        # Cropping
    crop_img = img[200:, :]
        # Resizing
    img = imresize(crop_img, size=image_size)
    imgs = []
    imgs.append(img)
    if len(imgs) < 1:
        print('Error no image at timestamp')

    img_block = np.stack(imgs, axis=0)
    if K.image_dim_ordering() == 'th':
        img_block = np.transpose(img_block, axes=(0, 3, 1, 2))
    return img_block

def image_translation(img, params):

    rows, cols, ch = img.shape

    M = np.float32([[1, 0, params[0]], [0, 1, params[1]]])
    dst = cv2.warpAffine(img, M, (cols, rows))
    return dst

def image_scale(img, params):

    res = cv2.resize(img, None, fx=params[0], fy=params[1], interpolation=cv2.INTER_CUBIC)
    return res

def image_shear(img, params):
    rows, cols, ch = img.shape
    factor = params*(-1.0)
    M = np.float32([[1, factor, 0], [0, 1, 0]])
    dst = cv2.warpAffine(img, M, (cols, rows))
    return dst

def image_rotation(img, params):
    rows, cols, ch = img.shape
    M = cv2.getRotationMatrix2D((cols/2, rows/2), params, 1)
    dst = cv2.warpAffine(img, M, (cols, rows))
    return dst

def image_contrast(img, params):
    alpha = params
    new_img = cv2.multiply(img, np.array([alpha]))                    # mul_img = img*alpha
    #new_img = cv2.add(mul_img, beta)                                  # new_img = img*alpha + beta

    return new_img

def image_brightness(img, params):
    beta = params
    new_img = cv2.add(img, beta)                                  # new_img = img*alpha + beta

    return new_img

def image_blur(img, params):
    blur = []
    if params == 1:
        blur = cv2.blur(img, (3, 3))
    if params == 2:
        blur = cv2.blur(img, (4, 4))
    if params == 3:
        blur = cv2.blur(img, (5, 5))
    if params == 4:
        blur = cv2.GaussianBlur(img, (3, 3), 0)
    if params == 5:
        blur = cv2.GaussianBlur(img, (5, 5), 0)
    if params == 6:
        blur = cv2.GaussianBlur(img, (7, 7), 0)
    if params == 7:
        blur = cv2.medianBlur(img, 3)
    if params == 8:
        blur = cv2.medianBlur(img, 5)
    if params == 9:
        blur = cv2.blur(img, (6, 6))
    if params == 10:
        blur = cv2.bilateralFilter(img, 9, 75, 75)
    return blur

def rotation(img, params):

    rows, cols, ch = img.shape
    M = cv2.getRotationMatrix2D((cols/2, rows/2), params[0], 1)
    dst = cv2.warpAffine(img, M, (cols, rows))
    return dst

def image_brightness1(img, params):
    w = img.shape[1]
    h = img.shape[0]
    if params > 0:
        for xi in range(0, w):
            for xj in range(0, h):
                if 255-img[xj, xi, 0] < params:
                    img[xj, xi, 0] = 255
                else:
                    img[xj, xi, 0] = img[xj, xi, 0] + params
                if 255-img[xj, xi, 1] < params:
                    img[xj, xi, 1] = 255
                else:
                    img[xj, xi, 1] = img[xj, xi, 1] + params
                if 255-img[xj, xi, 2] < params:
                    img[xj, xi, 2] = 255
                else:
                    img[xj, xi, 2] = img[xj, xi, 2] + params
    if params < 0:
        params = params*(-1)
        for xi in range(0, w):
            for xj in range(0, h):
                if img[xj, xi, 0] - 0 < params:
                    img[xj, xi, 0] = 0
                else:
                    img[xj, xi, 0] = img[xj, xi, 0] - params
                if img[xj, xi, 1] - 0 < params:
                    img[xj, xi, 1] = 0
                else:
                    img[xj, xi, 1] = img[xj, xi, 1] - params
                if img[xj, xi, 2] - 0 < params:
                    img[xj, xi, 2] = 0
                else:
                    img[xj, xi, 2] = img[xj, xi, 2] - params

    return img

def image_brightness2(img, params):
    beta = params
    b, g, r = cv2.split(img)
    b = cv2.add(b, beta)
    g = cv2.add(g, beta)
    r = cv2.add(r, beta)
    new_img = cv2.merge((b, g, r))
    return new_img

def main():
    pass
def deeptest(filepath,output_path):
    # for index in range(2,142):
    # output_path = 'F:/drive_scene/car'
    # filepath = 'F:/drive_scene/car/car.png'
    if '/' in filepath:
        filepath_list = filepath.split('/')
    else:
        filepath_list = filepath.split('\\')
    image_count = 0
    print('正在进行变异...')
    for index in range(2, 142):
        # translation
        if index / 2 >= 1 and index / 2 <= 10:
            # 生成文件夹
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/')
            p = index / 2
            params = [p * 10, p * 10]
            if index % 2 == 0:
                # for j in range(2000, 2100):
                # seed_image = cv2.imread(os.path.join(seed_inputs1, filelist1[j]))
                seed_image = cv2.imread(filepath)
                seed_image = image_translation(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                # seed_image = cv2.imread(os.path.join(seed_inputs1, filelist1[j]))
                seed_image = cv2.imread(filepath)
                seed_image = image_translation(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'translation/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
        # Scale 22-41
        if index / 2 >= 11 and index / 2 <= 20:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/')
            # for p in xrange(1,11):
            p = index / 2 - 10
            params = [p * 0.5 + 1, p * 0.5 + 1]
            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_scale(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_scale(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'scale/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
            # Shear 42-61
        if index / 2 >= 21 and index / 2 <= 30:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/')
            # for p in xrange(1,11):
            p = index / 2 - 20
            # for p in xrange(1,11):
            params = 0.1 * p

            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_shear(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_shear(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'shear/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
        # Rotation 62-81
        if index / 2 >= 31 and index / 2 <= 40:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/')
            p = index / 2 - 30
            # for p in xrange(1,11):
            params = p * 3

            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_rotation(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_rotation(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'rotation/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
                # model.predict_fn(seed_image, dummy=0)
                # model.predict_fn(seed_image, dummy=0)
        # Contrast 82-101
        if index / 2 >= 41 and index / 2 <= 50:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/')
            p = index / 2 - 40
            # or p in xrange(1,11):
            params = 1 + p * 0.2

            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_contrast(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_contrast(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'contrast/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
        # Brightness 102-121
        if index / 2 >= 51 and index / 2 <= 60:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/')
            p = index / 2 - 50
            # for p in xrange(1,11):
            params = p * 10

            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_brightness(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_brightness(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'brightness/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
        # blur 122-141
        if index / 2 >= 61 and index / 2 <= 70:
            if not os.path.exists(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/')
            if not os.path.exists(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/'):  # 若不存在路径则创建
                os.makedirs(
                    output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/')
            p = int(index / 2 - 60)
            # for p in xrange(1,11):
            params = p

            if index % 2 == 0:
                # for j in range(2000, 2100):
                seed_image = cv2.imread(filepath)
                seed_image = image_blur(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.imwrite('F:/autodrive_data/qrs/2/screenShot/198920/mutae.png', seed_image)
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # model.predict_fn(seed_image, dummy=0)
            else:
                # for j in range(2500, 2600):
                seed_image = cv2.imread(filepath)
                seed_image = image_blur(seed_image, params)
                if not os.path.exists(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                            image_count) + '/'):  # 若不存在路径则创建
                    os.makedirs(
                        output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                            image_count) + '/')
                cv2.imwrite(output_path + '/' + filepath_list[len(filepath_list) - 2] + '/' + 'blur/' + str(
                    image_count) + '/mutate.png', seed_image)
                image_count += 1
                # cv2.namedWindow("Image")
                # cv2.imshow("Image", seed_image)
                # cv2.waitKey(0)
                # cv2.imwrite('F:/autodrive_data/qrs/2/screenShot/198920/mutae.png',seed_image)
    print('变异完成~')


if __name__ == '__main__':
    # output_path = 'F:/drive_scene/deep_test'
    # filepath = 'F:/drive_scene/deeptest_original/198920/screenshot.png'
    # #deeptest(filepath,output_path)
    # deeptest('F:/drive_scene_5/original/1450380/screenshot.png','F:/drive_scene_5/deep_test')
    # deeptest('F:/drive_scene_7/original/926176/screenshot.png', 'F:/drive_scene_7/deep_test')
    # deeptest('F:/drive_scene_ajust/original/730684/screenshot.png','F:/drive_scene_ajust/deep_test')

    #deeptest('F:/drive_scene_car/original/331140/screenshot.png', 'F:/drive_scene_car/deep_test')

    #deeptest('F:/drive_scene/original/1511038/screenshot.png', 'F:/drive_scene/deep_test')
    result_dict = np.load('F:/drive_scene_7/mutants_add/0\screenShot/1969708/result_dict.npy', allow_pickle=True).item()
    print(len(result_dict.keys()))
    print(result_dict)


